package com.spzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.vo.ProductSkuVo;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.api.domain.UserBrowseHistory;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.domain.UserCollect;
import com.spzx.user.mapper.UserBrowseHistoryMapper;
import com.spzx.user.mapper.UserCollectMapper;
import com.spzx.user.mapper.UserInfoMapper;
import com.spzx.user.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-11-18
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Autowired
    private UserCollectMapper userCollectMapper;

    @Autowired
    private UserBrowseHistoryMapper userBrowseHistoryMapper;

    @Autowired
    private RemoteProductService remoteProductService;


    @Override
    public void register(UserInfo userInfo) {
        long count = baseMapper.selectCount(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getUsername, userInfo.getUsername())
        );
        if (count > 0) {
            throw new ServiceException("手机号已经存在");
        }
        baseMapper.insert(userInfo);
    }

    @Override
    public UserInfo selectUserByUserName(String username) {
        return baseMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getUsername, username)
        );
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setLastLoginTime(updateUserLogin.getLastLoginTime());
        baseMapper.updateById(userInfo);
        return true;
    }

    @Override
    public Boolean isCollect(Long skuId) {
        Long userId = SecurityUtils.getUserId();
        UserCollect userCollect = userCollectMapper.selectOne(
                new LambdaQueryWrapper<UserCollect>()
                        .eq(UserCollect::getUserId, userId)
                        .eq(UserCollect::getSkuId, skuId)
        );
        return userCollect != null;
    }

    @Override
    public Boolean collect(Long skuId) {
        Long userId = SecurityUtils.getUserId();
        UserCollect userCollect = new UserCollect();
        userCollect.setUserId(userId);
        userCollect.setSkuId(skuId);
        int insert = userCollectMapper.insert(userCollect);

        return insert > 0;
    }

    @Override
    public Boolean cancelCollect(Long skuId) {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //调用带条件的删除的方法
        int delete = userCollectMapper.delete(
                new LambdaQueryWrapper<UserCollect>()
                        .eq(UserCollect::getUserId, userId)
                        .eq(UserCollect::getSkuId, skuId));
        return delete > 0;
    }

    @Override
    public List<ProductSkuVo> getUserBrowseHistoryList() {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //根据用户id获取用户所有的浏览记录
        List<UserBrowseHistory> userBrowseHistories = userBrowseHistoryMapper.selectList(
                new LambdaQueryWrapper<UserBrowseHistory>()
                        .eq(UserBrowseHistory::getUserId, userId)
                        .orderByDesc(UserBrowseHistory::getUpdateTime));
        //获取所有的skuId
        List<Long> skuIdList = userBrowseHistories.stream().map(userBrowseHistory -> userBrowseHistory.getSkuId()).toList();
        //创建一个返回的集合
        List<ProductSkuVo> productSkuList = new ArrayList();
        //遍历
        skuIdList.forEach(skuId -> {
            //远程调用商品微服务获取商品Sku信息
            R<ProductSkuVo> productSkuById = remoteProductService.getProductVoSku(skuId, SecurityConstants.INNER);
            //获取商品sku
            ProductSkuVo productSku = productSkuById.getData();
            //放到集合中
            productSkuList.add(productSku);
        });
        return productSkuList;
    }

    /**
     * 获取用户收藏列表
     *
     * @return
     */
    @Override
    public List<ProductSkuVo> getUserCollectList() {
        //获取用户id
        Long userId = SecurityUtils.getUserId();

        //根据用户id获取用户所有的收藏记录
        List<UserCollect> userCollects = userCollectMapper.selectList(
                new LambdaQueryWrapper<UserCollect>()
                        .eq(UserCollect::getUserId, userId)
        );

        //获取所有的skuId
        List<Long> skuIdList = userCollects.stream().map(userCollect -> userCollect.getSkuId()).toList();

        //创建一个返回的集合
        List<ProductSkuVo> productSkuList = new ArrayList<>();

        //遍历
        skuIdList.forEach(skuId -> {
            //远程调用商品微服务获取商品Sku信息
            R<ProductSkuVo> productSkuById = remoteProductService.getProductVoSku(skuId, SecurityConstants.INNER);
            //获取商品sku
            ProductSkuVo productSku = productSkuById.getData();
            //放到集合中
            productSkuList.add(productSku);
        });
        return productSkuList;
    }
}
